package Leetcode第一期;

import java.util.ArrayList;
import java.util.List;

public class Z字形变换_6 {
    public String convert(String s, int numRows) {
        if(numRows<2) { return s; }
        //初始化
        List<StringBuffer> rows=new ArrayList<StringBuffer>();
        for(int i=0;i<numRows;i++){
            rows.add(new StringBuffer());
        }
        //索引位和标志位
        int i=0,flag=-1;
        for (char c : s.toCharArray()) {
            rows.get(i).append(c);
            if(i%numRows==0||i%numRows==numRows-1){flag=-flag;}
            i+=flag;
        }
        StringBuilder result=new StringBuilder();
        for (StringBuffer row:
             rows) {
            result.append(row.toString());
        }
        return result.toString();
    }
}
